<template>
  <div class="question-select">
    <el-form
      label-position="right"
      label-width="90px"
      :model="questionForm"
      ref="questionFormRef"
    >
      <el-row :gutter="5">
        <el-col :span="6"
          ><el-form-item label="学科" prop="subjectID">
            <el-select
              v-model="questionForm.subjectID"
              placeholder="请选择"
              @change="subjectChange"
            >
              <el-option
                v-for="(item, index) in subjectOption"
                :key="index"
                :label="item.label"
                :value="item.value"
              >
              </el-option>
            </el-select> </el-form-item
        ></el-col>
        <el-col :span="6"
          ><el-form-item label="二级目录" prop="catalogID">
            <el-select v-model="questionForm.catalogID" placeholder="请选择">
              <el-option
                v-for="(item, index) in directoryOption"
                :key="index"
                :label="item.label"
                :value="item.value"
              >
              </el-option>
            </el-select> </el-form-item
        ></el-col>
        <el-col :span="6"
          ><el-form-item label="标签" prop="tags">
            <el-select v-model="questionForm.tags" placeholder="请选择">
              <el-option
                v-for="(item, index) in tagsOption"
                :key="index"
                :label="item.label"
                :value="item.value"
              >
              </el-option> </el-select></el-form-item
        ></el-col>
        <el-col :span="6"
          ><el-form-item label="关键字" prop="keyword">
            <el-input
              v-model="questionForm.keyword"
              placeholder="请输入关键字"
            ></el-input></el-form-item
        ></el-col>
      </el-row>
      <el-row :gutter="10">
        <el-col :span="6"
          ><el-form-item label="试题类型" prop="questionType">
            <el-select v-model="questionForm.questionType" placeholder="请选择">
              <el-option
                v-for="(item, index) in questionType"
                :key="index"
                :label="item.label"
                :value="item.value"
              >
              </el-option>
            </el-select> </el-form-item
        ></el-col>
        <el-col :span="6"
          ><el-form-item label="难度" prop="difficulty">
            <el-select v-model="questionForm.difficulty" placeholder="请选择">
              <el-option
                v-for="(item, index) in difficulty"
                :key="index"
                :label="item.label"
                :value="item.value"
              >
              </el-option>
            </el-select> </el-form-item
        ></el-col>
        <el-col :span="6"
          ><el-form-item label="方向" prop="direction">
            <el-select v-model="questionForm.direction" placeholder="请选择">
              <el-option
                v-for="(item, index) in direction"
                :key="index"
                :label="item"
                :value="item"
              >
              </el-option>
            </el-select> </el-form-item
        ></el-col>
        <el-col :span="6"
          ><el-form-item label="录入人" prop="creatorID">
            <el-select v-model="questionForm.creatorID" placeholder="请选择">
              <el-option
                v-for="(item, index) in creatorOption"
                :key="index"
                :label="item.username"
                :value="item.id"
              >
              </el-option> </el-select></el-form-item
        ></el-col>
      </el-row>
      <el-row :gutter="10">
        <el-col :span="6"
          ><el-form-item label="题目备注" prop="remarks">
            <el-input v-model="questionForm.remarks"></el-input> </el-form-item
        ></el-col>
        <el-col :span="6"
          ><el-form-item label="企业简介" prop="shortName">
            <el-input
              v-model="questionForm.shortName"
            ></el-input> </el-form-item
        ></el-col>
        <el-col :span="6"
          ><el-form-item label="城市" class="double-select" prop="province">
            <el-select
              v-model="questionForm.province"
              placeholder="请选择"
              @change="provinceChange"
            >
              <el-option
                v-for="(item, index) in provinceOption"
                :key="index"
                :label="item"
                :value="item"
              >
              </el-option>
            </el-select>
            <el-select v-model="questionForm.city" placeholder="请选择">
              <el-option
                v-for="(item, index) in cityOption"
                :key="index"
                :label="item"
                :value="item"
              >
              </el-option> </el-select></el-form-item
        ></el-col>
        <el-col :span="6" class="select-btn">
          <el-button @click="clearForm">清除</el-button>
          <el-button type="primary" @click="onSearch">搜索</el-button>
        </el-col>
      </el-row>
    </el-form>
  </div>
</template>
<script>
import {
  list as subjectList,
  simple as simpleSubjectList,
} from '@/api/hmmm/subjects'
import {
  list as directoryList,
  simple as simpleDirectoryList,
} from '@/api/hmmm/directorys'
import { simple as simpleUser } from '@/api/base/users'
import { simple as tagsSimpleList } from '@/api/hmmm/tags'
import { provinces, citys } from '@/api/hmmm/citys'
import { difficulty, questionType, direction } from '@/api/hmmm/constants'
export default {
  name: 'QuestionSelect',
  data() {
    return {
      questionForm: {
        /* 选择框绑定数据 */
        subjectID: null, //学科级联框绑定数据
        catalogID: null, //二级目录级联选择框
        tags: null, //标签级联选择框绑定数据
        questionType: null, //试题类型
        keyword: null, //关键字
        difficulty: null, //难度
        province: null, //企业所在省份
        city: null, //企业所在城市
        remarks: null, //目录备注
        shortName: null, //企业简称
        direction: null, //方向
        creatorID: null, //录入人
      },
      /* 选择框选项数 */
      subjectOption: [], //学科级联选择框选项数据
      directoryOption: [], //二级目录级联选项数据
      tagsOption: [], //标签选择框选项数据
      difficulty,
      questionType,
      direction,
      creatorOption: [], //录入人选项数据
      provinceOption: provinces(),
      cityOption: [],
      Props: {
        expandTrigger: 'hover',
      },
      cityProps: {
        expandTrigger: 'hover',
      },
    }
  },
  methods: {
    //请求学科列表
    async getSubjectList() {
      try {
        const { data: res } = await simpleSubjectList()
        // console.log(res);
        this.subjectOption = res
        // this.$message.success('获取学科列表成功')
      } catch (error) {
        this.$message.error('获取学科列表失败')
      }
    },
    //获取用户列表
    async getUser() {
      try {
        const { data: res } = await simpleUser()
        this.creatorOption = res
      } catch (error) {
        console.log(error)
      }
    },
    //学科列表选择框发生变化
    async subjectChange() {
      this.tagsOption = []
      this.directoryOption = []
      try {
        //请求二级目录级联选择框选项数据
        const { data: directorys } = await simpleDirectoryList({
          subjectID: this.questionForm.subjectID.toString(),
        })
        //请求标签级联选择框选项数据
        const { data: tags } = await tagsSimpleList({
          subjectID: this.questionForm.subjectID.toString(),
        })
        // console.log(res)
        this.tagsOption = tags
        this.directoryOption = directorys
      } catch (error) {
        console.log(error)
        this.$message.error('获取二级目录失败')
      }
    },
    //省份选择框发生变化
    provinceChange() {
      this.cityOption = citys(this.questionForm.province)
      this.questionForm.city = ''
    },
    //清空搜索界面
    clearForm() {
      this.$refs.questionFormRef.resetFields()
      this.questionForm.city = null
      this.cityOption = []
      this.tagsOption = []
      this.directoryOption = []
      this.$emit('onSearch',this.questionForm) //重新加载数据
    },
    //搜索试题
    onSearch() {
      this.$emit('onSearch', this.questionForm)
    },
  },
  created() {
    this.getSubjectList()
    this.getUser()
  },
}
</script>
<style lang="less" scoped>
.question-select {
  .el-form-item {
    /deep/.el-form-item__content {
      display: flex;
      justify-content: space-between;
    }
    .el-select {
      width: 100%;
    }
  }
  .select-btn {
    display: flex;
    justify-content: flex-end;
  }
}
</style>